package cn.example.jpa.service;

import cn.example.jpa.dao.CarDao;
import cn.example.jpa.entity.Car;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CarserviceImpl implements CarService{
    @Autowired
    private CarDao carDao;

    @Override
    public Car get(Long id) {
        return carDao.findById(id).get();
    }

    @Override
    public Long add(Car car) {
        return carDao.save(car).getCarId();
    }

    @Override
    public int del(Long id) {
        carDao.deleteById(id);
        return 1;
    }

    @Override
    public Long update(Car car) {
        return carDao.save(car).getCarId();
    }

    @Override
    public Page<Car> getAll(Car car, PageRequest pageRequest) {
        ExampleMatcher matcher=ExampleMatcher.matching()
                .withMatcher("carName", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<Car> of=Example.of(car, matcher);
       return carDao.findAll(of, pageRequest);
    }
}
